/**
*****************************************************************************************
*     Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
*****************************************************************************************
* @file     fast_prov.h
* @brief    Header file for fast provision
* @details
* @author   hector_huang
* @date     2020-07-20
* @version  v1.0
* *************************************************************************************
*/
#ifndef _FAST_PROV_H_
#define _FAST_PROV_H_

#include "platform_types.h"
#include "gap_scan.h"
#include "app_msg.h"
#include "gap_manufacture_adv.h"
#include "mesh_api.h"

BEGIN_DECLS


/* #brief fast provision message number */
#define FAST_PROV_MSG_NUM                  8

typedef struct
{
    uint16_t msg_cache_size;
    uint8_t ttl;
    uint32_t pid;
    uint8_t secure_key[16];
    uint16_t unprov_adv_active_timeout;
    uint16_t unprov_adv_idle_timeout;
    uint16_t prov_adv_active_timeout;
    uint16_t prov_adv_idle_timeout;
    uint32_t adv_active_period;
    uint16_t dev_info_timeout;
    uint16_t dev_key_timeout;
    uint16_t prov_timeout;
    uint8_t relay_parallel_max;
} fast_prov_t;

extern fast_prov_t fast_prov;

/* fast provision device info callback */
typedef void (*fast_prov_dev_list_t)(uint8_t bt_addr_type, int8_t rssi, uint16_t trans_id,
                                     fast_prov_adv_t *padv);

/**
 * @brief initialize fast provision module
 * @param[in] evt_type: event type
 * @param[in] pevt_queue: event queue handle
 * @param[in] prov_cb: fast provision callback function
 * @return iniialize status
 */
bool fast_prov_init(uint8_t evt_type, void *pevt_queue, prov_cb_pf prov_cb);

/**
 * @brief handle advertising packet
 * @param[in] p_le_scan_info: scan information
 */
void fast_prov_handle_adv_pkt(T_LE_SCAN_INFO *p_le_scan_info);

/**
 * @brief fast provision inner message handle
 * @param[in] event: fast provision event type
 */
void fast_prov_inner_msg_handle(uint8_t event);

/**
 * @brief set network key
 * @param[in] key_refresh_flag: network key refresh flag
 * @param[in] net_key_index: network key index
 * @param[in] net_key: network key value
 */
void fast_prov_set_net_key(uint8_t key_refresh_flag, uint16_t net_key_index,
                           const uint8_t net_key[16]);

/**
 * @brief set application key
 * @param[in] app_key_index: application key index
 * @param[in] app_key: application key value
 */
void fast_prov_set_app_key(uint16_t app_key_index, const uint8_t app_key[16]);

/**
 * @brief set iv index
 * @param[in] iv_update_flag: iv index update flag
 * @param[in] iv_index: iv index
 */
void fast_prov_set_iv_index(uint8_t iv_update_flag, uint32_t iv_index);

/**
 * @brief set device address and device key
 * @param[in] mac_addr: device mac address
 * @param[in] addr: device unicast address
 * @param[in] dev_key: device key
 */
void fast_prov_set_dev_addr_key(uint8_t mac_addr[6], uint16_t addr, const uint8_t dev_key[16]);

/**
 * @brief enable or diable fast provision mode
 * @param[in] enable: TRUE enable, FALSE disable
 */
void fast_prov_mode_enable(bool enable);

/**
 * @brief handle gap message
 * @param[in] pmsg: gap message
 */
void fast_prov_handle_gap_msg(T_IO_MSG *pmsg);

/**
 * @brief list fast provision device
 * @param[in] pcb: device info list callback function
 * @param[in] p_le_scan_info: scan information
 */
void fast_prov_device_list(fast_prov_dev_list_t pcb, T_LE_SCAN_INFO *p_le_scan_info);

/**
 * @brief node unprovisioned
 */
void fast_prov_unprov(void);

/**
 * @brief enable or diable fast provision adv
 */
void fast_prov_adv_enable(bool flag);

END_DECLS

#endif /* _FAST_PROV_H_ */
